package com.gaotong.science.controller;



import com.gaotong.science.util.R;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@CrossOrigin
@RestController
@Component
@RequestMapping("/eduoss")
public class UploadController {
    @Value("${localFileUrl}")
    private String path;

//    @Value("${hostFileUrl}")
//    private String hostPath;
    @PostMapping("file")
    public String upload(MultipartFile file) throws IOException {


//        Date date = new Date();
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd ");
//        String dateFormat = simpleDateFormat.format(date);
        String originalFilename = file.getOriginalFilename();
//        Resource resource = file.getResource();
//        InputStream inputStream = file.getInputStream();

//        String suffixName = originalFilename.substring(0, originalFilename.lastIndexOf("."));
        String filePath = path+ "/";
        String fileName =originalFilename;

//        String uuid = UUID.randomUUID().toString().replace("-", "");
//        System.out.println(uuid);
//        uuid = uuid.substring(0, 6);
//        fileName = uuid+"-"+fileName;
        File dest = new File(filePath+fileName);

        if(!dest.getParentFile().exists()){
            dest.getParentFile().mkdirs();
        }
        try{
            file.transferTo(dest);
            return filePath+fileName;
        }catch (Exception e){
            System.out.println(e);
            return "上传失败";
        }


    }


}
